﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMOSOFT.Class;
using DevExpress.XtraEditors;
using COMOSOFT.OrderBusiness.Forms;
using COMOSOFT.Main;
using COMOSOFT.Main.Froms;
using Microsoft.Win32;
using System.IO;
using System.Deployment.Application;
using DevExpress.LookAndFeel;


namespace COMOSOFT.Main
{
    public partial class FrmLogin : Form
    {        
        RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
        public FrmLogin()
        {            
            InitializeComponent();
            this.Height = 305;

            #region [AutoUpdate]
            UpdateCheckInfo info = null;
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;

                try
                {
                    info = ad.CheckForDetailedUpdate();
                }
                catch (DeploymentDownloadException dde)
                {
                    MessageBox.Show("The new version of the application cannot be downloaded at this time. \n\nPlease check your network connection, or try again later. Error: " + dde.Message);
                    return;
                }
                catch (InvalidDeploymentException ide)
                {
                    MessageBox.Show("Cannot check for a new version of the application. The ClickOnce deployment is corrupt. Please redeploy the application and try again. Error: " + ide.Message);
                    return;
                }
                catch (InvalidOperationException ioe)
                {
                    MessageBox.Show("This application cannot be updated. It is likely not a ClickOnce application. Error: " + ioe.Message);
                    return;
                }

                if (info.UpdateAvailable)
                {
                    Boolean doUpdate = true;

                    if (!info.IsUpdateRequired)
                    {
                        //DialogResult dr = MessageBox.Show("An update is available. Would you like to update the application now?", "Update Available", MessageBoxButtons.OKCancel);
                        //if (!(DialogResult.OK == dr))
                        //{
                            doUpdate = true;
                       // }
                    }
                    else
                    {
                        // Display a message that the app MUST reboot. Display the minimum required version.
                        MessageBox.Show("This application has detected a mandatory update from your current " +
                            "version to version " + info.MinimumRequiredVersion.ToString() +
                            ". The application will now install the update and restart.",
                            "Update Available", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
                    }
                    if (doUpdate)
                    {
                        try
                        {                            
                            ad.UpdateCompleted += new AsyncCompletedEventHandler(ad_UpdateCompleted);                          
                            ad.UpdateProgressChanged += new DeploymentProgressChangedEventHandler(ad_UpdateProgressChanged);
                            ad.UpdateAsync();
                        }
                        catch (DeploymentDownloadException dde)
                        {
                            MessageBox.Show("Cannot install the latest version of the application. \n\nPlease check your network connection, or try again later. Error: " + dde);
                            return;
                        }
                    }
                }
            }
        }
         void ad_UpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage >= 0)
            {
                panel_Update.Visible = true;               
            }
            progressBar.EditValue = e.ProgressPercentage;
            lab_Updating.Text = "Updating new version..." + e.ProgressPercentage.ToString() + " %";
        }
         void ad_UpdateCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Cancelled == true)
            {
                MessageBox.Show("Cancelled");
            }
            else if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else
            {
                panel_Update.Visible = false;
                Application.Restart();
            }
           
        }
        #endregion

        bool IsClick = false;        
        FrmMain frmmain;
        private void Login()
        {
            var cls = new ClassShare("FrmLogin");
            if (cls.checklogin(txtaccount.EditValue.ToString(), txtpass.EditValue.ToString()))
            {
                DialogResult = DialogResult.OK;
                txtaccount.EditValue = null;
                txtpass.EditValue = null;
                if (frmmain == null)
                {
                    frmmain = new FrmMain();
                }
                frmmain.TopData();
                frmmain.Show();
                this.Hide();
            }
        }
        private void btnlogin_Click(object sender, EventArgs e)
        {
            splashScreenManager1.ShowWaitForm();            
            Login();
            splashScreenManager1.CloseWaitForm();
        }

        private void txtaccount_EditValueChanged(object sender, EventArgs e)
        {
            if (txtaccount.EditValue != null && txtpass.EditValue != null)
            {
                btnlogin.Enabled = true;
            }
            else
            {
                btnlogin.Enabled = false;
            }
        }

        private void txtpass_EditValueChanged(object sender, EventArgs e)
        {
            if (txtaccount.EditValue != null && txtpass.EditValue != null)
            {
                btnlogin.Enabled = true;
            }
            else
            {
                btnlogin.Enabled = false;
            }
        }
        private void Lab_ForgotPass_Click(object sender, EventArgs e)
        {
            FrmFogotPass FrmFGP = new FrmFogotPass();
            FrmFGP.ShowDialog();
        }

        private void bnt_Options_Click(object sender, EventArgs e)
        {
            if (IsClick == false)
            {
                this.Height = 400;
                IsClick = true;
                bnt_Options.Image = imageCollection1.Images[1];              
            }
            else
            {
                this.Height = 305;
                IsClick = false;
                bnt_Options.Image = imageCollection1.Images[0];
            }
        }

        private void checkEdit_Start_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEdit_Start.Checked == true)
                rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());
            else
                rkApp.DeleteValue("MyApp", false);
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {            
            if (File.Exists(ClassShare.lcSystem + @"\config.dat"))
            {                
                BinaryReader binrd = new BinaryReader(new FileStream(ClassShare.lcSystem + @"\config.dat", FileMode.Open));
                txtaccount.Text = binrd.ReadString();
                txtpass.Text = binrd.ReadString();
                checkEdit_Remember.Checked = binrd.ReadBoolean();
                binrd.Close();
                //Login();
            }
        }

        private void checkEdit_Remember_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEdit_Remember.Checked == true)
                ClassShare.IsRemember = true;
            else
                ClassShare.IsRemember = false;
        }
    }
}
